package com.gopay.common.user.dao.impl;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.gopay.common.constants.cust.RelSourceConstans;

import org.springframework.stereotype.Repository;

import com.gopay.common.base.dao.impl.BaseDAOImpl;
import com.gopay.common.domain.cloud.CloudCustAcctRel;
import com.gopay.common.domain.cps.CpsGenMainOrder;
import com.gopay.common.order.dao.CommonQueryDAO;
import com.gopay.common.user.dao.CloudCustAcctRelDao;

/**
 * @author boai
 * @date 2016年3月12日 下午4:18:15
 * www.gopay.com.cn Inc.All rights reserved.
 */
@Repository("liveWalletCustAcctRelDao")
public class LiveWalletCustAcctRelDaoImpl extends BaseDAOImpl<CloudCustAcctRel, Serializable> implements
		CloudCustAcctRelDao {
	
	 @Resource(name = "commonQueryDAO")
	 private CommonQueryDAO commonQueryDAO;
	
	/**
	 * 根据平台客户号和子公司帐号获取关联实体
	 * @param partnerCustId  平台custId
	 * @param acctId  子公司账户
	 */
	@Override
    public CloudCustAcctRel findCloudCustAcctRelByAcct(String partnerCustId, String acctId){
		
		String hql="from CloudCustAcctRel t where t.acctId=:acctId and t.partnerCustId=:partnerCustId";
		Map<String, Object> map=new HashMap<String,Object>();
		
		map.put("partnerCustId", partnerCustId);
		map.put("acctId", acctId);
		
		List<CloudCustAcctRel> result=this.findByHql(hql, map);
		return result!=null&&result.size()>0?result.get(0):null;
	}
	/**
	 * 通过协议号获取关联信息
	 * @param agreeId
	 * @return
	 */
	@Override
    public CloudCustAcctRel findCloudCustAcctRelByAgreeId(String agreeId){
		String hql="from CloudCustAcctRel t where t.agreeId=:agreeId";
		Map<String, Object> map=new HashMap<String,Object>();
		map.put("agreeId", agreeId);
		
		List<CloudCustAcctRel> result=this.findByHql(hql, map);
		return result!=null&&result.size()>0?result.get(0):null;
	}
	
	/**
	 * 根据子公司客户号和子公司的唯一Id现为邮箱获取关联实体
	 * @param custId  子公司custId
	 * @param partnerUserId  子公司账户
	 */
	@Override
    public CloudCustAcctRel findCloudCustAcctRelByUserId(String custId, String partnerUserId){
		
		String hql="from CloudCustAcctRel t where t.custId=:custId and t.partnerUserId=:partnerUserId";
		Map<String, Object> map=new HashMap<String,Object>();
		
		map.put("custId", custId);
		map.put("partnerUserId", partnerUserId);
		
		List<CloudCustAcctRel> result=this.findByHql(hql, map);
		return result!=null&&result.size()>0?result.get(0):null;
	}
	/**
	 * 根据采购部客户号和子公司的唯一Id现为邮箱获取关联实体
	 * @param partnerCustId  平台custId
	 * @param partnerUserId  邮箱
	 */
	@Override
    public CloudCustAcctRel findCloudPaCustAcctRelByUserId(String partnerCustId, String partnerUserId){
		
		String hql="from CloudCustAcctRel t where t.partnerCustId=:partnerCustId and t.partnerUserId=:partnerUserId";
		Map<String, Object> map=new HashMap<String,Object>();
		
		map.put("partnerCustId", partnerCustId);
		map.put("partnerUserId", partnerUserId);
		
		List<CloudCustAcctRel> result=this.findByHql(hql, map);
		return result!=null&&result.size()>0?result.get(0):null;
	}
	/**
	 * 根据合作方客户号、合作方账户号和用户协议号确定唯一关联实体
	 * @param partnerCustId  平台custId
	 * @param partnerAcctId  平台账户
	 * @param agreeId  协议号
	 */
	@Override
    public CloudCustAcctRel findCustAcctRelByAcctIdCustIdAndAgreeId(String partnerCustId, String partnerAcctId, String agreeId){
		
		String hql="from CloudCustAcctRel t where t.partnerCustId=:partnerCustId and t.partnerAcctId=:partnerAcctId and t.agreeId=:agreeId";
		Map<String, Object> map=new HashMap<String,Object>();
		
		map.put("partnerCustId", partnerCustId);
		map.put("partnerAcctId", partnerAcctId);
		map.put("agreeId", agreeId);
		
		List<CloudCustAcctRel> result=this.findByHql(hql, map);
		return result!=null&&result.size()>0?result.get(0):null;
	}
	/**
	 * 根据平台custId、子custId、子账户号确定唯一关联实体
	 * @param partnerCustId  平台custId
	 * @param partnerAcctId  平台账户
	 * @param custId  子custId
	 * @param acctId  子账户号
	 */
	@Override
    public CloudCustAcctRel findCustAcctRelByAcctIdCustIdNew(String partnerCustId, String partnerAcctId, String custId, String acctId){
		
		String hql="from CloudCustAcctRel t where t.stat=1 and t.partnerCustId=:partnerCustId and t.partnerAcctId=:partnerAcctId and t.custId=:custId and t.acctId=:acctId";
		Map<String, Object> map=new HashMap<String,Object>();
		
		map.put("partnerCustId", partnerCustId);
		map.put("partnerAcctId", partnerAcctId);
		map.put("custId", custId);
		map.put("acctId", acctId);
		
		List<CloudCustAcctRel> result=this.findByHql(hql, map);
		return result!=null&&result.size()>0?result.get(0):null;
	}
	/**
	 * 根据合作方账户号和用户Id确定唯一关联实体
	 * @param partnerAcctId  平台账户
	 * @param mobileOrEmail  用户手机或邮箱
	 */	
	@Override
    public CloudCustAcctRel findCustAcctRelByAcctIdAndUserMob(String partnerAcctId, String mobileOrEmail){
		String hql="from CloudCustAcctRel t where t.stat=1 and t.partnerAcctId=:partnerAcctId and t.partnerUserId=:partnerUserId";
		Map<String, Object> map=new HashMap<String,Object>();	
		map.put("partnerAcctId", partnerAcctId);
		map.put("partnerUserId", mobileOrEmail);		
		List<CloudCustAcctRel> result=this.findByHql(hql, map);
		return result!=null&&result.size()>0?result.get(0):null;
	}

	/**
	 * 根据合作方客户号、合作方商户的客户号 并且是来源于生钱包的签约
	 * @param partnerCustId
	 * @param custId
	 * @return
	 */
	@Override
	public CloudCustAcctRel findCustAcctRelByPartnerCustIdAndCustId(String partnerCustId, String custId, String transferOutAcctId, String transferInAcctId) {
		String hql = "from CloudCustAcctRel t where t.stat=1 and t.source=:source and t.partnerCustId=:partnerCustId " +
				"and t.custId=:custId and t.partnerAcctId=:transferOutAcctId and t.acctId=:transferInAcctId";
		Map<String, Object> map = new HashMap<>();
		map.put("partnerCustId", partnerCustId);
		map.put("custId", custId);
		map.put("source", RelSourceConstans.LIVE_WALLET);
		map.put("transferOutAcctId", transferOutAcctId);
		map.put("transferInAcctId", transferInAcctId);
		List<CloudCustAcctRel> result=this.findByHql(hql, map);
		return result!=null&&result.size()>0?result.get(0):null;
	}
	/**
	 * 根据合作方客户号，合作方商户的客户号确定唯一关联实体
	 * @param custId
	 * @param partnerCustId
	 * @return
	 */
	@Override
    public CloudCustAcctRel findCustAcctRelByCustIdAndPartnerCustId(String custId, String partnerCustId) {
		String hql = "from CloudCustAcctRel t where t.stat=1 and t.partnerCustId=:partnerCustId and t.custId=:custId";
		Map<String, Object> map = new HashMap<>();
		map.put("partnerCustId", partnerCustId);
		map.put("custId", custId);
		List<CloudCustAcctRel> result = this.findByHql(hql, map);
		return result != null && result.size() > 0 ? result.get(0) : null;
	}
	@Override
	public String getCloudAcct(CpsGenMainOrder mainOrder) {
		String sql = "select t.gateway_source,t.gopay_order_id from  cps_gen_main_order t where t.gopay_order_id=:orderId";
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("orderId", mainOrder.getGopayOrderId());
		List<Object[]> rsList = commonQueryDAO.findBySql(sql, map);

		if (rsList != null && rsList.size() > 0) {
			return  (String)rsList.get(0)[0];
		}
		return null;
	}
	@Override
	public String getCloudCustId(CpsGenMainOrder mainOrder) {
		// TODO Auto-generated method stub
		return null;
	}
    @Override
    public String getCloudAcct(String acctId, String merId) {
        // TODO Auto-generated method stub
        return null;
    }

	@Override
	public CloudCustAcctRel findCustAcctRealByAgreeIdAndPartnerCustId(String agreeId, String partnerCustId) {
		String hql = "from CloudCustAcctRel t where t.agreeId=:agreeId and t.partnerCustId=:partnerCustId";
		Map<String, Object> map = new HashMap<String, Object>();

		map.put("agreeId", agreeId);
		map.put("partnerCustId", partnerCustId);

		List<CloudCustAcctRel> result = this.findByHql(hql, map);
		return result != null && result.size() > 0 ? result.get(0) : null;
	}


	@Override
	public CloudCustAcctRel findCustAcctRealByPartnerUserIdAndPartnerCustId(String partnerUserId, String partnerCustId) {
		String hql = "from CloudCustAcctRel t where t.partnerUserId=:partnerUserId and t.partnerCustId=:partnerCustId";
		Map<String, Object> map = new HashMap<String, Object>();

		map.put("partnerUserId", partnerUserId);
		map.put("partnerCustId", partnerCustId);

		List<CloudCustAcctRel> result = this.findByHql(hql, map);
		return result != null && result.size() > 0 ? result.get(0) : null;
	}


	@Override
	public List<CloudCustAcctRel> findCloudCustAcctRelList(String custId) {
		return null;
	}

	@Override
	public CloudCustAcctRel findByCustId(String custId) {
		return null;
	}
	 
	@Override
	public List<CloudCustAcctRel> findSubCloudAccts(String partnerCustId, String partnerAcctId) {
		
		return null;
	}
}
